<template>
  <div class="panel-tab__content">
    <el-form size="mini" label-width="auto" @submit.native.prevent>
      <el-form-item :label="$t('loopCharacteristics')">
        <el-select v-model="loopCharacteristics" @change="changeLoopCharacteristicsType">
          <!--bpmn:MultiInstanceLoopCharacteristics-->
          <el-option :label="$t('parallelMultipleEvents')" value="ParallelMultiInstance" />
          <el-option :label="$t('timeorderMultiEvents')" value="SequentialMultiInstance" />
          <!--bpmn:StandardLoopCharacteristics-->
          <el-option :label="$t('loopEvent')" value="StandardLoop" />
          <el-option :label="$t('none')" value="Null" />
        </el-select>
      </el-form-item>
      <template
        v-if="loopCharacteristics === 'ParallelMultiInstance' || loopCharacteristics === 'SequentialMultiInstance'">
        <el-form-item :label="$t('loopBasicNum')" key="loopCardinality">
          <el-input v-model="loopInstanceForm.loopCardinality" clearable @change="updateLoopCardinality" />
        </el-form-item>
        <el-form-item :label="$t('collection')" key="collection">
          <el-input v-model="loopInstanceForm.collection" clearable @change="updateLoopBase" />
        </el-form-item>
        <el-form-item :label="$t('elementVariable')" key="elementVariable">
          <el-input v-model="loopInstanceForm.elementVariable" clearable @change="updateLoopBase" />
        </el-form-item>
        <el-form-item :label="$t('completionCondition')" key="completionCondition">
          <el-input v-model="loopInstanceForm.completionCondition" clearable @change="updateLoopCondition" />
        </el-form-item>
        <el-form-item :label="$t('async')" key="async">
          <el-checkbox
            v-model="loopInstanceForm.asyncBefore"
            :label="$t('asyncBefore')"
            @change="updateLoopAsync('asyncBefore')" />
          <el-checkbox
            v-model="loopInstanceForm.asyncAfter"
            :label="$t('asyncAfter')"
            @change="updateLoopAsync('asyncAfter')" />
          <el-checkbox
            v-model="loopInstanceForm.exclusive"
            v-if="loopInstanceForm.asyncAfter || loopInstanceForm.asyncBefore"
            :label="$t('exclude')"
            @change="updateLoopAsync('exclusive')" />
        </el-form-item>
        <el-form-item
          :label="$t('timeCycle')"
          prop="timeCycle"
          v-if="loopInstanceForm.asyncAfter || loopInstanceForm.asyncBefore"
          key="timeCycle">
          <el-input v-model="loopInstanceForm.timeCycle" clearable @change="updateLoopTimeCycle" />
        </el-form-item>
      </template>
    </el-form>
  </div>
</template>

<script>
export default {
  name: 'ElementMultiInstance',
  props: {
    // eslint-disable-next-line vue/require-default-prop
    businessObject: Object,
    // eslint-disable-next-line vue/require-default-prop
    type: String
  },
  inject: {
    prefix: 'prefix'
  },
  data () {
    return {
      loopCharacteristics: '',
      // 默认配置，用来覆盖原始不存在的选项，避免报错
      defaultLoopInstanceForm: {
        completionCondition: '',
        loopCardinality: '',
        extensionElements: [],
        asyncAfter: false,
        asyncBefore: false,
        exclusive: false
      },
      loopInstanceForm: {}
    }
  },
  watch: {
    businessObject: {
      immediate: true,
      handler (val) {
        this.bpmnElement = window.bpmnInstances.bpmnElement
        this.getElementLoop(val)
      }
    }
  },
  methods: {
    getElementLoop (businessObject) {
      if (!businessObject.loopCharacteristics) {
        this.loopCharacteristics = 'Null'
        this.loopInstanceForm = {}
        return
      }
      if (businessObject.loopCharacteristics.$type === 'bpmn:StandardLoopCharacteristics') {
        this.loopCharacteristics = 'StandardLoop'
        this.loopInstanceForm = {}
        return
      }
      if (businessObject.loopCharacteristics.isSequential) {
        this.loopCharacteristics = 'SequentialMultiInstance'
      } else {
        this.loopCharacteristics = 'ParallelMultiInstance'
      }
      // 合并配置
      let completionConditionBody = ''
      let loopCardinalityBody = ''
      if (businessObject.loopCharacteristics) {
        if (businessObject.loopCharacteristics.completionCondition) {
          completionConditionBody = businessObject.loopCharacteristics.completionCondition
        }
        if (businessObject.loopCharacteristics.loopCardinality) {
          loopCardinalityBody = businessObject.loopCharacteristics.loopCardinality
        }
      }
      this.loopInstanceForm = {
        ...this.defaultLoopInstanceForm,
        ...businessObject.loopCharacteristics,
        completionCondition: completionConditionBody,
        loopCardinality: loopCardinalityBody
      }
      // 保留当前元素 businessObject 上的 loopCharacteristics 实例
      this.multiLoopInstance = window.bpmnInstances.bpmnElement.businessObject.loopCharacteristics
      // 更新表单
      if (
        businessObject.loopCharacteristics.extensionElements &&
        businessObject.loopCharacteristics.extensionElements.values &&
        businessObject.loopCharacteristics.extensionElements.values.length
      ) {
        // eslint-disable-next-line max-len
        this.$set(this.loopInstanceForm, 'timeCycle', businessObject.loopCharacteristics.extensionElements.values[0].body)
      }
    },
    changeLoopCharacteristicsType (type) {
      // this.loopInstanceForm = { ...this.defaultLoopInstanceForm }; // 切换类型取消原表单配置
      // 取消多实例配置
      if (type === 'Null') {
        window.bpmnInstances.modeling.updateProperties(this.bpmnElement, { loopCharacteristics: null })
        return
      }
      // 配置循环
      if (type === 'StandardLoop') {
        const loopCharacteristicsObject = window.bpmnInstances.moddle.create('bpmn:StandardLoopCharacteristics')
        window.bpmnInstances.modeling.updateProperties(this.bpmnElement, {
          loopCharacteristics: loopCharacteristicsObject
        })
        this.multiLoopInstance = null
        return
      }
      // 时序
      if (type === 'SequentialMultiInstance') {
        // eslint-disable-next-line max-len
        this.multiLoopInstance = window.bpmnInstances.moddle.create('bpmn:MultiInstanceLoopCharacteristics', { isSequential: true })
      } else {
        this.multiLoopInstance = window.bpmnInstances.moddle.create('bpmn:MultiInstanceLoopCharacteristics')
      }
      window.bpmnInstances.modeling.updateProperties(this.bpmnElement, {
        loopCharacteristics: this.multiLoopInstance
      })
    },
    // 循环基数
    updateLoopCardinality (cardinality) {
      let loopCardinality = null
      if (cardinality && cardinality.length) {
        loopCardinality = window.bpmnInstances.moddle.create('bpmn:FormalExpression', { body: cardinality })
      }
      // eslint-disable-next-line max-len
      window.bpmnInstances.modeling.updateModdleProperties(this.bpmnElement, this.multiLoopInstance, { loopCardinality })
    },
    // 完成条件
    updateLoopCondition (condition) {
      let completionCondition = null
      if (condition && condition.length) {
        completionCondition = window.bpmnInstances.moddle.create('bpmn:FormalExpression', { body: condition })
      }
      // eslint-disable-next-line max-len
      window.bpmnInstances.modeling.updateModdleProperties(this.bpmnElement, this.multiLoopInstance, { completionCondition })
    },
    // 重试周期
    updateLoopTimeCycle (timeCycle) {
      const extensionElements = window.bpmnInstances.moddle.create('bpmn:ExtensionElements', {
        values: [
          window.bpmnInstances.moddle.create(`${this.prefix}:FailedJobRetryTimeCycle`, {
            body: timeCycle
          })
        ]
      })
      // eslint-disable-next-line max-len
      window.bpmnInstances.modeling.updateModdleProperties(this.bpmnElement, this.multiLoopInstance, { extensionElements })
    },
    // 直接更新的基础信息
    updateLoopBase () {
      window.bpmnInstances.modeling.updateModdleProperties(this.bpmnElement, this.multiLoopInstance, {
        collection: this.loopInstanceForm.collection || null,
        elementVariable: this.loopInstanceForm.elementVariable || null
      })
    },
    // 各异步状态
    updateLoopAsync (key) {
      const { asyncBefore, asyncAfter } = this.loopInstanceForm
      let asyncAttr = Object.create(null)
      if (!asyncBefore && !asyncAfter) {
        this.$set(this.loopInstanceForm, 'exclusive', false)
        asyncAttr = { asyncBefore: false, asyncAfter: false, exclusive: false, extensionElements: null }
      } else {
        asyncAttr[key] = this.loopInstanceForm[key]
      }
      window.bpmnInstances.modeling.updateModdleProperties(this.bpmnElement, this.multiLoopInstance, asyncAttr)
    }
  },
  beforeDestroy () {
    this.multiLoopInstance = null
    this.bpmnElement = null
  }
}
</script>
